package com.zhb.parse;

import com.zhb.beans.Goods;
import com.zhb.beans.SongList;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: XianYvTest
 * @description:
 * @author: rainboz
 * @create: 2019-09-20 19:51
 **/
public class GoodsParse {
    public static List<Goods> getData(String entity) {
        //获取的数据，存放在集合中
        List<Goods> data = new ArrayList<Goods>();
        //采用Jsoup解析
        Document doc = Jsoup.parse(entity);
        //获取html标签中的内容
        Elements elements = doc.select("div[class=item-list-wrap]").select("a[class=item-wrap]");
        for (Element ele : elements) {
            /**
             * backgroundImage;
             * contentTitle;
             * price;class=item-info class=price-line class=price class=price-value
             * wantNum;class=item-info class=price-line class=want-num
             * userInfo;div:class=user-line p:class=user-name
             */
            String backgroundImage = ele.select("div[class=item-img]").attr("style");
            String contentTitle = ele.select("div[class=item-info]").select("p[class=item-title]").text();
            String price = ele.select("div[class=item-info]").
                    select("div[class=price-line]").select("div[class=price]").
                    select("p[class=price-value]").text();
            String wantNum = ele.select("div[class=item-info]").
                    select("div[class=price-line]").
                    select("p[class=want-num]").text();
            String userInfo = ele.select("div[class=user-line]").select("p[class=user-name]").text();
            Goods goods = new Goods(backgroundImage,contentTitle,price,wantNum,userInfo);
            data.add(goods);
            System.out.println("songList:======================= " + goods);
        }
        System.out.println("data==========================" + data);
        return data;
    }
}
